package com.itheima.thread.pool.demo03;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorDemo02 {

    public static void main(String[] args) throws InterruptedException {

        // 使用ThreadPoolExecutor去创建一个线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 2 , 6 , TimeUnit.SECONDS ,
                new ArrayBlockingQueue<Runnable>(3) , Executors.defaultThreadFactory() , new ThreadPoolExecutor.DiscardPolicy()) ;

        /**
         * ThreadPoolExecutor.AbortPolicy: 	           丢弃任务并抛出RejectedExecutionException异常。是默认的策略。
         * ThreadPoolExecutor.DiscardPolicy： 		   丢弃任务，但是不抛出异常 这是不推荐的做法。
         */
        // 提交任务，可以提交的任务总数5个
        for(int x = 0 ; x < 6 ; x++) {
            threadPoolExecutor.submit(() -> {

                System.out.println(Thread.currentThread().getName() + "----执行了任务...." );

                // 目的：让这个线程一直处于占用状态
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }) ;
        }

    }

}
